package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

// 성별은 성별 항목이 홀수이면 "남자", 짝수이면 "여자"로 표시
public class StringTest2 extends JFrame {

	private JPanel contentPane;
	private JTextField str;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTextArea result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StringTest2 frame = new StringTest2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public StringTest2() {
		setTitle("주민번호");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getStr());
		contentPane.add(getLblNewLabel());
		contentPane.add(getBtnNewButton());
		contentPane.add(getScrollPane());
	}
	private JTextField getStr() {
		if (str == null) {
			str = new JTextField();
			str.setBounds(119, 35, 284, 21);
			str.setColumns(10);
		}
		return str;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("주민번호 입력");
			lblNewLabel.setBounds(24, 38, 83, 15);
		}
		return lblNewLabel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("확인");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						// 입력값이 없으면 오류 메세지
						String temp = str.getText();
						if(temp.equals("")){
							result.setText("주민등록번호를 입력");
							return;
						}
						String y = temp.substring(0,2);
						String m = temp.substring(2,4);
						String d = temp.substring(4,6);
						int gender = Integer.parseInt(temp.substring(7,8));
						
						StringBuilder sb = new StringBuilder();
						sb.append("생년:"+y);
						sb.append("\n생월:"+m);
						sb.append("\n생일:"+d);
						
						// 성별 계산
						String g = (gender%2==0)? "여자":"남자";
						sb.append("\n성별:"+g);
						
						result.setText(sb.toString());
						
						
						
//						String temp = str.getText();
//						String r = temp.substring(0,2);
//						 result.append("생년:"+r);
//						 result.append("\n");
//						 String temps = str.getText();
//						 String t = temps.substring(2,4);
//							 result.append("생월:"+t);
//							 result.append("\n");
//						 String temp1 = str.getText();
//						 String s = temp1.substring(4,6);
//							 result.append("생일:"+s);
//							 result.append("\n");
//							 int a = Integer.parseInt(str.getText(7,8));
//							 if(a%2==1) result.append("여자"); 
//							 else{ result.append("남자");
//							 
//							 
//					}
		                }catch(Exception ex){
						result.append("\n값을 입력하시오");
					}
				}
			});
			btnNewButton.setBounds(24, 83, 379, 23);
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(25, 140, 378, 95);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
}
